<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:carStateInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:carStateInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:carStateInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:carStateInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="carStateInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--优化为车牌号-->
      <el-table-column label="车辆id" align="center" prop="carId" />
      <el-table-column label="车速" align="center" prop="speed" />
      <el-table-column label="节气门开度" align="center" prop="jqmKd" />
      <el-table-column label="电源电压" align="center" prop="dyDy" />
      <el-table-column label="发动机转速" align="center" prop="fdjZs" />
      <el-table-column label="发动机负载" align="center" prop="fdjFh" />
      <el-table-column label="发动机运行时间" align="center" prop="fdjRunTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.fdjRunTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发动机机油油位" align="center" prop="fdjJyyw" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.flag_open" :value="scope.row.fdjJyyw"/>
        </template>
      </el-table-column>
      <el-table-column label="冷却液温度" align="center" prop="lqyWd" />
      <el-table-column label="废气再循环阀设定位置" align="center" prop="fqzxhfSdwz" />
      <el-table-column label="废气再循环阀实际位置" align="center" prop="fqzxhfSjwz" />
      <el-table-column label="废气再循环阀反馈电压" align="center" prop="fqzxhfFkdy" />
      <el-table-column label="氧传感器超过中值次数" align="center" prop="ycgqcgzzCs" />
      <el-table-column label="喷油脉宽" align="center" prop="pyMk" />
      <el-table-column label="空气流量" align="center" prop="kqLl" />
      <el-table-column label="进气歧管绝对压力" align="center" prop="jqqgJdyl" />
      <el-table-column label="进气温度" align="center" prop="jqWd" />
      <el-table-column label="总计缺火故障" align="center" prop="zjqhgz" />
      <el-table-column label="自动变速器油温度" align="center" prop="zdbsqyWd" />
      <el-table-column label="变速器油温度" align="center" prop="bsqyWd" />
      <el-table-column label="变矩器锁止离合器" align="center" prop="bjqszlhq">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.flag_open" :value="scope.row.bjqszlhq"/>
        </template>
      </el-table-column>
      <el-table-column label="变速器" align="center" prop="bsq" />
      <el-table-column label="左前车轮转速" align="center" prop="zqClzs" />
      <el-table-column label="右前车轮转速" align="center" prop="yqClzs" />
      <el-table-column label="左后车轮转速" align="center" prop="zhClzs" />
      <el-table-column label="右后车轮转速" align="center" prop="yhClzs" />
      <el-table-column label="ABS电压" align="center" prop="absDy" />
      <el-table-column fixed='right' label="采集时间" align="center" prop="createAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createAt) }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:carStateInfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleCheck(scope.row)"
          >监测车况</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:carStateInfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改车况信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="160px">
        <!--优化为车牌号-->
        <el-form-item label="车辆id" prop="carId">
          <el-input v-model="form.carId" placeholder="请输入车辆id" />
        </el-form-item>
        <el-form-item label="故障码" prop="errorCode">
          <el-input v-model="form.errorCode" placeholder="请输入故障码: 逗号分割" />
        </el-form-item>
        <el-form-item label="车速" prop="speed">
          <el-input v-model="form.speed" placeholder="请输入车速" />
        </el-form-item>
        <el-form-item label="节气门开度" prop="jqmKd">
          <el-input v-model="form.jqmKd" placeholder="请输入节气门开度" />
        </el-form-item>
        <el-form-item label="电源电压" prop="dyDy">
          <el-input v-model="form.dyDy" placeholder="请输入电源电压" />
        </el-form-item>
        <el-form-item label="发动机转速" prop="fdjZs">
          <el-input v-model="form.fdjZs" placeholder="请输入发动机转速" />
        </el-form-item>
        <el-form-item label="发动机负载" prop="fdjFh">
          <el-input v-model="form.fdjFh" placeholder="请输入发动机负载" />
        </el-form-item>
        <el-form-item label="发动机运行时间" prop="fdjRunTime">
          <el-date-picker clearable
            v-model="form.fdjRunTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择发动机运行时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="发动机机油油位" prop="fdjJyyw">
          <el-select v-model="form.fdjJyyw" placeholder="机油油位状态">
            <el-option
              v-for="dict in dict.type.flag_open"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="冷却液温度" prop="lqyWd">
          <el-input v-model="form.lqyWd" placeholder="请输入冷却液温度" />
        </el-form-item>
        <el-form-item label="废气再循环阀设定位置" prop="fqzxhfSdwz">
          <el-input v-model="form.fqzxhfSdwz" placeholder="请输入废气再循环阀设定位置" />
        </el-form-item>
        <el-form-item label="废气再循环阀实际位置" prop="fqzxhfSjwz">
          <el-input v-model="form.fqzxhfSjwz" placeholder="请输入废气再循环阀实际位置" />
        </el-form-item>
        <el-form-item label="废气再循环阀反馈电压" prop="fqzxhfFkdy">
          <el-input v-model="form.fqzxhfFkdy" placeholder="请输入废气再循环阀反馈电压" />
        </el-form-item>
        <el-form-item label="氧传感器超过中值次数" prop="ycgqcgzzCs">
          <el-input v-model="form.ycgqcgzzCs" placeholder="请输入氧传感器超过中值次数" />
        </el-form-item>
        <el-form-item label="喷油脉宽" prop="pyMk">
          <el-input v-model="form.pyMk" placeholder="请输入喷油脉宽" />
        </el-form-item>
        <el-form-item label="空气流量" prop="kqLl">
          <el-input v-model="form.kqLl" placeholder="请输入空气流量" />
        </el-form-item>
        <el-form-item label="进气歧管绝对压力" prop="jqqgJdyl">
          <el-input v-model="form.jqqgJdyl" placeholder="请输入进气歧管绝对压力" />
        </el-form-item>
        <el-form-item label="进气温度" prop="jqWd">
          <el-input v-model="form.jqWd" placeholder="请输入进气温度" />
        </el-form-item>
        <el-form-item label="总计缺火故障" prop="zjqhgz">
          <el-input v-model="form.zjqhgz" placeholder="请输入总计缺火故障" />
        </el-form-item>
        <el-form-item label="自动变速器油温度" prop="zdbsqyWd">
          <el-input v-model="form.zdbsqyWd" placeholder="请输入自动变速器油温度" />
        </el-form-item>
        <el-form-item label="变速器油温度" prop="bsqyWd">
          <el-input v-model="form.bsqyWd" placeholder="请输入变速器油温度" />
        </el-form-item>
        <el-form-item label="变矩器锁止离合器" prop="bjqszlhq">
          <el-select v-model="form.bjqszlhq" placeholder="请选择变矩器锁止离合器状态 开/关">
            <el-option
              v-for="dict in dict.type.flag_open"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="变速器" prop="bsq">
          <el-input v-model="form.bsq" placeholder="请输入变速器" />
        </el-form-item>
        <el-form-item label="左前车轮转速" prop="zqClzs">
          <el-input v-model="form.zqClzs" placeholder="请输入左前车轮转速" />
        </el-form-item>
        <el-form-item label="右前车轮转速" prop="yqClzs">
          <el-input v-model="form.yqClzs" placeholder="请输入右前车轮转速" />
        </el-form-item>
        <el-form-item label="左后车轮转速" prop="zhClzs">
          <el-input v-model="form.zhClzs" placeholder="请输入左后车轮转速" />
        </el-form-item>
        <el-form-item label="右后车轮转速" prop="yhClzs">
          <el-input v-model="form.yhClzs" placeholder="请输入右后车轮转速" />
        </el-form-item>
        <el-form-item label="ABS电压" prop="absDy">
          <el-input v-model="form.absDy" placeholder="请输入ABS电压" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改检测结果对话框 -->
    <el-dialog title="车况信息" :visible.sync="resultOpen" width="500px" append-to-body>
      <el-form ref="resultForm" :model="resultForm" :rules="rules" label-width="80px">
        <el-form-item label="故障数" prop="errorNum">
          <el-input v-model="resultForm.errorNum"/>
        </el-form-item>
        <el-form-item label="故障信息" prop="errorInfo">
          <el-input v-model="resultForm.errorInfo" type="textarea" :autosize="{ minRows: 2, maxRows: 10}"/>
        </el-form-item>
        <el-form-item label="创建时间" prop="createAt">
          <el-input readonly v-model="resultForm.createAt"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCarStateInfo, getCarStateInfo, delCarStateInfo, addCarStateInfo, updateCarStateInfo } from "@/api/system/carStateInfo";
import {listCars} from "@/api/system/cars";
import {checkCarStateInfo} from "@/api/system/carStateInfo";
import {listResults} from "@/api/system/results";

export default {
  name: "CarStateInfo",
  dicts: ['flag_open'],
  data() {
    return {
      carsList: [],

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车况信息表格数据
      carStateInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        carId: null,
        errorCode: null,
        speed: null,
        jqmKd: null,
        dyDy: null,
        fdjZs: null,
        fdjFh: null,
        fdjRunTime: null,
        fdjJyyw: null,
        lqyWd: null,
        fqzxhfSdwz: null,
        fqzxhfSjwz: null,
        fqzxhfFkdy: null,
        ycgqcgzzCs: null,
        pyMk: null,
        kqLl: null,
        jqqgJdyl: null,
        jqWd: null,
        zjqhgz: null,
        zdbsqyWd: null,
        bsqyWd: null,
        bjqszlhq: null,
        bsq: null,
        zqClzs: null,
        yqClzs: null,
        zhClzs: null,
        yhClzs: null,
        absDy: null,
        createAt: null
      },
      // 表单参数
      form: {},
      resultForm: {},
      resultOpen: false,
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getCarList();
  },
  methods: {
    /** 查看检查结果 */
    handleCheck(row) {
      this.resultReset();
      this.loading = true;
      checkCarStateInfo(row).then(res => {
        console.log(row.infoId);
        this.resultForm.stateInfoId = row.infoId;
        listResults(this.resultForm).then(response => {
          this.resultForm = response.rows[0];
          console.log(this.resultForm);
          console.log(response.rows);
          this.resultOpen = true;
          this.loading = false;
        });
      })
    },
    /** 查询车辆信息表列表 */
    getCarList() {
      this.loading = true;
      listCars().then(response => {
        this.carsList = response.rows;
        this.loading = false;
      });
    },
    /** 查询车况信息列表 */
    getList() {
      this.loading = true;
      listCarStateInfo(this.queryParams).then(response => {
        this.carStateInfoList = response.rows;
        console.log(this.carStateInfoList);
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.resultOpen = false;
      this.reset();
      this.resultReset();
    },
    // 表单重置
    reset() {
      this.form = {
        infoId: null,
        carId: null,
        errorCode: null,
        speed: null,
        jqmKd: null,
        dyDy: null,
        fdjZs: null,
        fdjFh: null,
        fdjRunTime: null,
        fdjJyyw: null,
        lqyWd: null,
        fqzxhfSdwz: null,
        fqzxhfSjwz: null,
        fqzxhfFkdy: null,
        ycgqcgzzCs: null,
        pyMk: null,
        kqLl: null,
        jqqgJdyl: null,
        jqWd: null,
        zjqhgz: null,
        zdbsqyWd: null,
        bsqyWd: null,
        bjqszlhq: null,
        bsq: null,
        zqClzs: null,
        yqClzs: null,
        zhClzs: null,
        yhClzs: null,
        absDy: null,
        createAt: null
      };
      this.resetForm("form");
    },
    resultReset() {
      this.resultForm = {
        resId: null,
        stateInfoId: null,
        errorNum: null,
        errorInfo: null,
        createAt: null
      };
      this.resetForm("resultForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.infoId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加车况信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const infoId = row.infoId || this.ids
      getCarStateInfo(infoId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改车况信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.infoId != null) {
            updateCarStateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCarStateInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const infoIds = row.infoId || this.ids;
      this.$modal.confirm('是否确认删除车况信息编号为"' + infoIds + '"的数据项？').then(function() {
        return delCarStateInfo(infoIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/carStateInfo/export', {
        ...this.queryParams
      }, `carStateInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
